/*    */ package com.zimbra.cs.util.yauth;
/*    */ 
/*    */ import java.io.IOException;
/*    */ import java.util.HashMap;
/*    */ import org.apache.log4j.Logger;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public final class RawAuthManager
/*    */ {
/*    */   private final TokenStore store;
/*    */   private final HashMap<String, RawAuth> cookies;
/* 28 */   private static final Logger LOG = Logger.getLogger(RawAuthManager.class);
/*    */   
/*    */   public RawAuthManager(TokenStore store) {
/* 31 */     this.store = store;
/* 32 */     this.cookies = new HashMap();
/*    */   }
/*    */   
/*    */   public RawAuth authenticate(String appId, String user, String pass) throws AuthenticationException, IOException
/*    */   {
/* 37 */     RawAuth auth = (RawAuth)this.cookies.get(key(appId, user));
/* 38 */     if ((auth == null) || (auth.isExpired()))
/*    */     {
/* 40 */       String token = this.store.getToken(appId, user);
/* 41 */       if (token == null)
/*    */       {
/* 43 */         token = this.store.newToken(appId, user, pass);
/*    */       }
/*    */       try {
/* 46 */         auth = RawAuth.authenticate(appId, token);
/*    */       }
/*    */       catch (AuthenticationException e)
/*    */       {
/* 50 */         switch (e.getErrorCode()) {
/*    */         case TOKEN_REQUIRED: 
/*    */         case INVALID_TOKEN: 
/* 53 */           invalidate(appId, user);
/* 54 */           token = this.store.newToken(appId, user, pass);
/* 55 */           auth = RawAuth.authenticate(appId, token);
/*    */         }
/*    */       }
/* 58 */       throw e;
/*    */       
/*    */ 
/*    */ 
/* 62 */       this.cookies.put(key(appId, user), auth);
/*    */     }
/* 64 */     return auth;
/*    */   }
/*    */   
/*    */   public void invalidate(String appId, String user) {
/* 68 */     this.cookies.remove(key(appId, user));
/*    */   }
/*    */   
/*    */ 
/*    */   public Authenticator newAuthenticator(final String appId, final String user, final String pass)
/*    */   {
/* 74 */     new Authenticator() {
/*    */       public RawAuth authenticate() throws AuthenticationException, IOException {
/* 76 */         return RawAuthManager.this.authenticate(appId, user, pass);
/*    */       }
/*    */       
/* 79 */       public void invalidate() { RawAuthManager.this.invalidate(appId, user); }
/*    */     };
/*    */   }
/*    */   
/*    */   public TokenStore getTokenStore()
/*    */   {
/* 85 */     return this.store;
/*    */   }
/*    */   
/*    */   private String key(String appId, String user) {
/* 89 */     return appId + " " + user;
/*    */   }
/*    */   
/*    */   public String toString() {
/* 93 */     return String.format("{cookies=%d,tokens=%d}", new Object[] { Integer.valueOf(this.cookies.size()), Integer.valueOf(this.store.size()) });
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/yauth/RawAuthManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */